package org.pjsip.manager;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

import com.blankj.utilcode.util.SizeUtils;

import org.pjsip.pjsua2.app.MyApplication;

import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

/**
 * author : GWFan
 * time   : 1/20/21 3:18 PM
 * desc   :
 */

public class MyWindowManager {

    private static final String TAG = "MyWindowParamsManager";
    private static WindowManager.LayoutParams layoutParams;

    public static WindowManager getWindowManager() {
        return (WindowManager) MyApplication.getInstance().getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
    }

    public static WindowManager.LayoutParams initWMParams() {
        if (layoutParams == null) {
            synchronized (MyWindowManager.class) {
                if (layoutParams == null) {
                    layoutParams = new WindowManager.LayoutParams();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                    } else {
                        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
                                | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
                    }
                }
            }
        }
        return layoutParams;
    }

    public static WindowManager.LayoutParams getWMParams(int height) {
        WindowManager.LayoutParams layoutParams = initWMParams();
        layoutParams.y = 0;
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | FLAG_NOT_TOUCH_MODAL;
        layoutParams.gravity = Gravity.TOP;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        if (height <= 0) {
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        } else {
            layoutParams.height = SizeUtils.dp2px(height);
        }
        return layoutParams;
    }
}

